package com.itheima.reggie.handler;

import com.itheima.reggie.commom.CustomException;
import com.itheima.reggie.commom.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Sea
 * @date 2023/3/2 16:23
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //unique重复异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultInfo duplicateKeyExceptionHandler(Exception exception){
        log.error(exception.getMessage());
        return ResultInfo.error("名称重复");
    }
    @ExceptionHandler(CustomException.class)
    public ResultInfo exceptionHandler(CustomException exception){
        log.error(exception.getMessage());
        return ResultInfo.error(exception.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public ResultInfo exceptionHandler(Exception exception)
    {
        log.error(exception.getMessage());
        return ResultInfo.error("对不起，网络出了点小问题，请稍再试");
    }
}
